<?php
namespace Admin\Controller;
use Think\Controller;
class ProductsController extends CommonController  {
 public function index(){
       $conds = array();
       $products_name = $_GET['products_name'];
        if($products_name) {
        $conds['products_name'] = $products_name;
        $this->assign('products_name',$conds['products_name']);
        }
  $page = $_REQUEST['p'] ? $_REQUEST['p'] : 1;
  $pageSize = 6;
  $products = D("Products")->getProducts($conds,$page,$pageSize);
  $count = D("Products")->getProductsCount($conds);
  $res  =  new \Think\Page($count,$pageSize);
  $pageres = $res->show();
  $this->assign('pageres',$pageres);
  $this->assign('products',$products);
  $this->display();
}

 	public function add(){
    if($_POST) {
         if(!isset($_POST['products_name']) || !$_POST['products_name']) {
                return show(0,'产品名称不存在');
            }
            if(!isset($_POST['small_title']) || !$_POST['small_title']) {
                return show(0,'短标题不存在');
            }
            if(!isset($_POST['keywords']) || !$_POST['keywords']) {
                return show(0,'关键字不存在');
            }
            if(!isset($_POST['content']) || !$_POST['content']) {
                return show(0,'content不存在');
            }
           if($_POST['products_id']) {
                return $this->save($_POST);
            }
            $productsId= D("Products")->insert($_POST); 
            if($productsId){
              $productsContentData['content'] = $_POST['content'];
              $productsContentData['products_id'] = $productsId;
              $cId = D("ProductsContent")->insert($productsContentData);

              if($cId){
                 return  show(1,"新增成功");
              }else{
                 return  show(1,"主表插入成功，副表插入失败");
              }
              }else{
                 return  show(0,"新增失败");
              }
        }else {

          $this->display();
        }
    }
   public function edit(){
      $productsId =$_GET['id'];
      if(!$productsId) {
            // 执行跳转
            $this->redirect('/admin.php?c=products');
      }
    $products = D("Products")->find($productsId);
   if(!$products) {
            $this->redirect('/admin.php?c=products');
        }
      $productsContent = D("ProductsContent")->find($productsId);
        if($productsContent) {
            $products['content'] = $productsContent['content'];
        }
       $this->assign('products',$products);

        $this->display();


    }

    public function save($data) {
        $productsId = $data['products_id'];

        unset($data['products_id']);

        try {
            $id = D("Products")->updateById($productsId, $data);
            $productsContentData['content'] = $data['content'];
            $condId = D("ProductsContent")->updateProductsById($productsId, $productsContentData);
            if($id === false || $condId === false) {
                return show(0, '更新失败');
            }
            return show(1, '更新成功');
        }catch(Exception $e) {
            return show(0, $e->getMessage());
        }

    }

     public function setStatus() {
        try {
            if ($_POST) {
                $id = $_POST['id'];
                $status = $_POST['status'];
                if (!$id) {
                    return show(0, 'ID不存在');
                }
                $res = D("Products")->updateStatusById($id, $status);
                if ($res) {
                    return show(1, '操作成功');
                } else {
                    return show(0, '操作失败');
                }
            }
            return show(0, '没有提交的内容');
        }catch(Exception $e) {
            return show(0, $e->getMessage());
        }
    }
public function listorder() {
       $listorder = $_POST['listorder'];
        $jumpUrl = $_SERVER['HTTP_REFERER'];
        $errors = array();
        try {
            if ($listorder) {
                foreach ($listorder as $productsId => $v) {
                    // 执行更新
                    $id = D("Products")->updateProductsListorderById($productsId, $v);
                    if ($id === false) {
                        $errors[] = $productsId;
                    }
                }
                if ($errors) {
                    return show(0, '排序失败-' . implode(',', $errors), array('jump_url' => $jumpUrl));
                }
                return show(1, '排序成功', array('jump_url' => $jumpUrl));
            }
        }catch (Exception $e) {
            return show(0, $e->getMessage());
        }
        return show(0,'排序数据失败',array('jump_url' => $jumpUrl));
    }

} 